<template>
  <div style="margin: auto; width: 90%; margin-top: 3%">
    <el-row>
      <el-col :span="19"
        ><el-input
          clearable
          placeholder="请输入内容"
          prefix-icon="el-icon-search"
          v-model="input2"
          style="width: 20%"
          @keyup.enter.native="init"
        ></el-input
      ></el-col>
      <el-col :span="3"
        ><el-button type="warning" @click="employeeAdd"
          >添加员工</el-button
        ></el-col
      >
    </el-row>

    <el-table border:true :data="tableData" style="width: 100%" stripe>
      <el-table-column prop="name" label="员工姓名" />
      <el-table-column prop="username" label="账号" />
      <el-table-column prop="phone" label="手机号" />
      <el-table-column label="账号状态">
        <template v-slot="{ row }">
          {{ row.status == 1 ? "正常" : "已禁用" }}
        </template>
      </el-table-column>
      <el-table-column label="操作">
        <template v-slot="{ row }">
          <el-link type="primary" @click="handleEdit(row)">编辑</el-link>
          <el-divider direction="vertical"></el-divider>
          <el-popconfirm
            title="确认调整该账号的状态?"
            @onConfirm="handleChangeStatus(row)"
          >
            <el-link slot="reference" type="danger">{{
              row.status == 1 ? "禁用" : "启用"
            }}</el-link>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="pageInfo.page"
      :page-sizes="[10, 20, 30, 40]"
      :page-size="pageInfo.pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
      style="text-align: center; margin-top: 3%"
    >
    </el-pagination>
  </div>
</template>

<script>
import { getEmployees, changeStatus } from "@/api/employee";
export default {
  data() {
    return {
      tableData: [],
      pageInfo: { page: 1, pageSize: 10 },
      total: 0,
      input2: "",
    };
  },
  created() {
    this.init();
  },
  methods: {
    init() {
      getEmployees({ ...this.pageInfo, name: this.input2 }).then((res) => {
        const { records, total } = res.data;
        this.tableData = records;
        this.total = total;
      });
    },
    handleSizeChange(val) {
      this.pageInfo.page = 1;
      this.pageInfo.pageSize = val;
      this.init();
    },
    handleCurrentChange(val) {
      this.pageInfo.page = val;
      this.init();
    },
    async handleChangeStatus(row) {
      await changeStatus({ id: row.id, status: +!row.status });
      this.$message.success("改变成功");
      this.init();
    },
    employeeAdd() {
      this.$router.push("/backend/employee/add");
      this.$router.push({
        path: "/backend/employee/add",
      });
    },
    handleEdit(row) {
      this.$router.push({
        path: "/backend/employee/edit",
        query: { id: row.id },
      });
    },
  },
};
</script>